package com.cjl.dental_clinic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjl.dental_clinic.base.BaseController;
import com.cjl.dental_clinic.base.ResVo;
import com.cjl.dental_clinic.entity.UserConfig;
import com.cjl.dental_clinic.Req.UserConfigReq;
import com.cjl.dental_clinic.service.UserConfigService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: cjl
 * @time: 2024/03/12 17:12
 */
@RestController
@RequestMapping("/cjl/userConfig")
public class UserConfigController extends BaseController<UserConfig, UserConfigService> {

    public UserConfigController(UserConfigService baseService) {
        super(baseService);
    }

    /**
     * 字典列表
     * @return
     */
    @PostMapping("list")
    public ResVo<List<UserConfigReq>> getUserConfigReq(){
        return ResVo.ok(baseService.listUserConfigReq());
    }

    /**
     * 字典添加
     * @param userReq
     * @return
     */
    @PostMapping("add")
    public ResVo<Boolean> addUserConfig(UserConfigReq userReq){
        UserConfig userConfig = new UserConfig();
        userConfig.setName(userReq.getName())
                .setParentId(userReq.getParentId());
        userConfig.initCreateUser();
        return ResVo.ok(baseService.save(userConfig));
    }

    /**
     * 删除字典
     * @param id
     * @return
     */
    @PostMapping("del")
    public ResVo<Boolean> delUserConfig(Integer id){
        QueryWrapper<UserConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<UserConfig> list = baseService.list(wrapper);
        if (!list.isEmpty()){
            return ResVo.ok("删除失败，检查是否有子类",false);
        }
        return ResVo.ok(baseService.removeById(id));
    }

    /**
     * 根据类别查询
     * @param type
     * @return
     */
    @PostMapping("type")
    public ResVo<List<UserConfig>> typeList(Integer type){
        QueryWrapper<UserConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",type);
        List<UserConfig> list = baseService.list(wrapper);
        return ResVo.ok(list);
    }

    /**
     * 修改
     * @param userConfig
     * @return
     */
    @PostMapping("updata")
    public ResVo<Boolean> upData(UserConfig userConfig){
        return ResVo.ok(baseService.updateById(userConfig));
    }
}
